package com.xiaopeng.common.secure;

import java.security.KeyPair;
import java.util.Base64;

public class CryptoDemo {
    public static void main(String[] args) {
        try {
            // AES示例
            String aesKey = CryptoUtils.generateAESKey();
            String iv = CryptoUtils.generateAESKey(); // 实际应用中IV应随机生成
            String plainText = "Hello, AES!";

            String encrypted = CryptoUtils.aesEncrypt(plainText, aesKey, iv);
            String decrypted = CryptoUtils.aesDecrypt(encrypted, aesKey, iv);

            System.out.println("AES加密前: " + plainText);
            System.out.println("AES加密后: " + encrypted);
            System.out.println("AES解密后: " + decrypted);

            // RSA示例
            KeyPair keyPair = CryptoUtils.generateRSAKeyPair();
            String publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
            String privateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());
            System.out.println("RSA公钥: " + publicKey);
            System.out.println("RSA私钥: " + privateKey);

            plainText = "Hello, RSA!";
            encrypted = CryptoUtils.rsaEncrypt(plainText, publicKey);
            decrypted = CryptoUtils.rsaDecrypt(encrypted, privateKey);

            System.out.println("\nRSA加密前: " + plainText);
            System.out.println("RSA加密后: " + encrypted);
            System.out.println("RSA解密后: " + decrypted);

            // 签名验签示例
            String signature = CryptoUtils.rsaSign(plainText, privateKey);
            boolean isValid = CryptoUtils.rsaVerify(plainText, signature, publicKey);

            System.out.println("\n签名: " + signature);
            System.out.println("验签结果: " + isValid);

            // 哈希示例
            String sha256Hash = CryptoUtils.sha256(plainText);
            String md5Hash = CryptoUtils.md5(plainText);

            System.out.println("\nSHA-256: " + sha256Hash);
            System.out.println("MD5: " + md5Hash);

        } catch (CryptoUtils.CryptoException e) {
            e.printStackTrace();
        }
    }
}
